package com.envision.controller.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.envision.controller.dao.MainPageDao;
import com.envision.controller.domain.EcDayInfoDO;
import com.envision.controller.domain.EcMonthInfoDO;
import com.envision.controller.domain.LoadDayInfoDO;
import com.envision.controller.domain.TrnsfrmrLoadValueDO;
import com.envision.controller.result.GetLatelyInfoRes;
import com.envision.controller.result.Lately7DayEc;
import com.envision.controller.result.TheDayEc;
import com.envision.controller.result.ThisDayLoad;
import com.envision.controller.result.ThisMonthEcCons;
import com.envision.controller.result.ThisMonthTranLoad;
import com.envision.controller.service.MainPageService;
import com.envision.user.service.impl.UserServiceImpl;
import com.envision.utils.CommonCode;

@Service("mainPageServiceImpl")
public class MainPageServiceImpl implements MainPageService{

	private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	@Autowired
	private MainPageDao mainPageDao;

	@Override
	public GetLatelyInfoRes getLateLyInfo() {
		logger.info(">>>>>>>>>>主页面最近用电负荷数据查询开始。");
		GetLatelyInfoRes getLatelyInfoRes = new GetLatelyInfoRes();
		try {
			
			/**
			 * 本月变压器负载数据
			 */
			//T1-1
			TrnsfrmrLoadValueDO trnsfrmrLoadValueDOT1_1 = 
					mainPageDao.geTrnsfrmrLoadValueDOs(CommonCode.floorCode_T1,CommonCode.transCode_1);
			//T1-2
			TrnsfrmrLoadValueDO trnsfrmrLoadValueDOT1_2 = 
					mainPageDao.geTrnsfrmrLoadValueDOs(CommonCode.floorCode_T1,CommonCode.transCode_2);
			//T1-3
			TrnsfrmrLoadValueDO trnsfrmrLoadValueDOT1_3 = 
					mainPageDao.geTrnsfrmrLoadValueDOs(CommonCode.floorCode_T1,CommonCode.transCode_3);
			//T1-4
			TrnsfrmrLoadValueDO trnsfrmrLoadValueDOT1_4 = 
					mainPageDao.geTrnsfrmrLoadValueDOs(CommonCode.floorCode_T1,CommonCode.transCode_4);
			//T2-1
			TrnsfrmrLoadValueDO trnsfrmrLoadValueDOT2_1 =
					mainPageDao.geTrnsfrmrLoadValueDOs(CommonCode.floorCode_T2,CommonCode.transCode_1);
			//T2-2
			TrnsfrmrLoadValueDO trnsfrmrLoadValueDOT2_2 = 
					mainPageDao.geTrnsfrmrLoadValueDOs(CommonCode.floorCode_T2,CommonCode.transCode_2);
			//T2-3
			TrnsfrmrLoadValueDO trnsfrmrLoadValueDOT2_3 = 
					mainPageDao.geTrnsfrmrLoadValueDOs(CommonCode.floorCode_T2,CommonCode.transCode_3);
			//T2-4
			TrnsfrmrLoadValueDO trnsfrmrLoadValueDOT2_4 = 
					mainPageDao.geTrnsfrmrLoadValueDOs(CommonCode.floorCode_T2,CommonCode.transCode_4);
			List<ThisMonthTranLoad> thisMonthTranLoadList = new ArrayList<ThisMonthTranLoad>();
			ThisMonthTranLoad thisMonthTranLoad1 = new ThisMonthTranLoad();
			if(trnsfrmrLoadValueDOT1_1 != null) {
				thisMonthTranLoad1.setLoadName("T1-1");
				thisMonthTranLoad1.setLoadValue(trnsfrmrLoadValueDOT1_1.getTheMonthMaxLoad()+"");
			}else {
				thisMonthTranLoad1.setLoadName("T1-1");
				thisMonthTranLoad1.setLoadValue("");
			}
			thisMonthTranLoadList.add(thisMonthTranLoad1);
			
			ThisMonthTranLoad thisMonthTranLoad2 = new ThisMonthTranLoad();
			if(trnsfrmrLoadValueDOT1_2 != null) {
				thisMonthTranLoad2.setLoadName("T1-2");
				thisMonthTranLoad2.setLoadValue(trnsfrmrLoadValueDOT1_2.getTheMonthMaxLoad()+"");
			}else {
				thisMonthTranLoad2.setLoadName("T1-2");
				thisMonthTranLoad2.setLoadValue("");
			}
			thisMonthTranLoadList.add(thisMonthTranLoad2);
			
			ThisMonthTranLoad thisMonthTranLoad3 = new ThisMonthTranLoad();
			if(trnsfrmrLoadValueDOT1_3 != null) {
				thisMonthTranLoad3.setLoadName("T1-3");
				thisMonthTranLoad3.setLoadValue(trnsfrmrLoadValueDOT1_3.getTheMonthMaxLoad()+"");
			}else {
				thisMonthTranLoad3.setLoadName("T1-3");
				thisMonthTranLoad3.setLoadValue("");
			}
			thisMonthTranLoadList.add(thisMonthTranLoad3);
			
			ThisMonthTranLoad thisMonthTranLoad4 = new ThisMonthTranLoad();
			if(trnsfrmrLoadValueDOT1_4 != null) {
				thisMonthTranLoad4.setLoadName("T1-4");
				thisMonthTranLoad4.setLoadValue(trnsfrmrLoadValueDOT1_4.getTheMonthMaxLoad()+"");
			}else {
				thisMonthTranLoad4.setLoadName("T1-4");
				thisMonthTranLoad4.setLoadValue("");
			}
			thisMonthTranLoadList.add(thisMonthTranLoad4);
			
			ThisMonthTranLoad thisMonthTranLoad5 = new ThisMonthTranLoad();
			if(trnsfrmrLoadValueDOT2_1 != null) {
				thisMonthTranLoad5.setLoadName("T2-1");
				thisMonthTranLoad5.setLoadValue(trnsfrmrLoadValueDOT2_1.getTheMonthMaxLoad()+"");
			}else {
				thisMonthTranLoad5.setLoadName("T2-1");
				thisMonthTranLoad5.setLoadValue("");
			}
			thisMonthTranLoadList.add(thisMonthTranLoad5);
			
			ThisMonthTranLoad thisMonthTranLoad6 = new ThisMonthTranLoad();
			if(trnsfrmrLoadValueDOT2_2 != null) {
				thisMonthTranLoad6.setLoadName("T2-2");
				thisMonthTranLoad6.setLoadValue(trnsfrmrLoadValueDOT2_2.getTheMonthMaxLoad()+"");
			}else {
				thisMonthTranLoad6.setLoadName("T2-2");
				thisMonthTranLoad6.setLoadValue("");
			}
			thisMonthTranLoadList.add(thisMonthTranLoad6);
			
			ThisMonthTranLoad thisMonthTranLoad7 = new ThisMonthTranLoad();
			if(trnsfrmrLoadValueDOT2_3 != null) {
				thisMonthTranLoad7.setLoadName("T2-3");
				thisMonthTranLoad7.setLoadValue(trnsfrmrLoadValueDOT2_3.getTheMonthMaxLoad()+"");
			}else {
				thisMonthTranLoad7.setLoadName("T2-3");
				thisMonthTranLoad7.setLoadValue("");
			}
			thisMonthTranLoadList.add(thisMonthTranLoad7);
			
			ThisMonthTranLoad thisMonthTranLoad8 = new ThisMonthTranLoad();
			if(trnsfrmrLoadValueDOT2_4 != null) {
				thisMonthTranLoad8.setLoadName("T2-4");
				thisMonthTranLoad8.setLoadValue(trnsfrmrLoadValueDOT2_4.getTheMonthMaxLoad()+"");
			}else {
				thisMonthTranLoad8.setLoadName("T2-4");
				thisMonthTranLoad8.setLoadValue("");
			}
			thisMonthTranLoadList.add(thisMonthTranLoad8);
			
			logger.info(">>>>>>>>>>本月变压器负载数据查询成功。");
			getLatelyInfoRes.setThisMonthTranLoadList(thisMonthTranLoadList);
			
			/**
			 * 本月电量构成
			 */
			ThisMonthEcCons thisMonthEcCons = new ThisMonthEcCons();
			//制冷系统
			EcMonthInfoDO refrSystem = mainPageDao.getThisMonthEcCons(CommonCode.systemRfrgrtnCode);
			//小动力
			EcMonthInfoDO smallPower = mainPageDao.getThisMonthEcCons(CommonCode.systemSmallPowerCode);
			//AHU
			EcMonthInfoDO ahuSystem = mainPageDao.getThisMonthEcCons(CommonCode.systemAhuCode);
			//照明系统
			EcMonthInfoDO lightSystem = mainPageDao.getThisMonthEcCons(CommonCode.systemLightingCode);
			//IDF用电
			EcMonthInfoDO idfEc = mainPageDao.getThisMonthEcCons(CommonCode.systemIdfCode);
			//其它
			EcMonthInfoDO ohterEc = mainPageDao.getThisMonthEcCons(CommonCode.systemOtherCode);
			if(refrSystem != null) {
				thisMonthEcCons.setRefrSystem(refrSystem.getElctrctyCnsmptn());
			}else {
				thisMonthEcCons.setRefrSystem(0);
			}
			if(smallPower != null) {
				thisMonthEcCons.setSmallPower(smallPower.getElctrctyCnsmptn());
			}else {
				thisMonthEcCons.setSmallPower(0);
			}
			if(ahuSystem != null) {
				thisMonthEcCons.setAhuSystem(ahuSystem.getElctrctyCnsmptn());
			}else {
				thisMonthEcCons.setAhuSystem(0);
			}
			if(lightSystem != null) {
				thisMonthEcCons.setLightSystem(lightSystem.getElctrctyCnsmptn());
			}else {
				thisMonthEcCons.setLightSystem(0);
			}
			if(idfEc != null) {
				thisMonthEcCons.setIdfEc(idfEc.getElctrctyCnsmptn());
			}else {
				thisMonthEcCons.setIdfEc(0);
			}
			if(ohterEc != null) {
				thisMonthEcCons.setOhterEc(ohterEc.getElctrctyCnsmptn());
			}else {
				thisMonthEcCons.setOhterEc(0);
			}
			logger.info(">>>>>>>>>>本月电量构成数据查询成功。");
			getLatelyInfoRes.setThisMonthEcCons(thisMonthEcCons);
			
			/**
			 * 最近七天用电情况
			 */
			Lately7DayEc lately7DayEc = new Lately7DayEc();
			//日期计算
			SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
			Date date = new Date();
			Calendar c = Calendar.getInstance();
			c.setTime(date); 
			int day = c.get(Calendar.DATE); 
			//今天
			EcDayInfoDO ecDayInfoDOthisDayEc = mainPageDao.getLately7DayEc(sdf.format(date));
			TheDayEc thisDayEc = new TheDayEc();
			if(ecDayInfoDOthisDayEc != null) {
				thisDayEc.setEcDate(ecDayInfoDOthisDayEc.getElecUseDate());
				thisDayEc.setElctrctyCnsmptn(ecDayInfoDOthisDayEc.getElctrctyCnsmptn());
				thisDayEc.setOtdrTmprtr(ecDayInfoDOthisDayEc.getOtdrTmprtr());
				thisDayEc.setRingRatio(ecDayInfoDOthisDayEc.getRingRatio());
			}else {
				thisDayEc.setEcDate("");
				thisDayEc.setElctrctyCnsmptn(0);
				thisDayEc.setOtdrTmprtr(0);
				thisDayEc.setRingRatio(0);
			}
			
			lately7DayEc.setThisDayEc(thisDayEc);
			//前一天
			c.set(Calendar.DATE,day-1); 
			String front1Day = sdf.format(c.getTime()); 
			EcDayInfoDO ecDayInfoDOfront1DayEc = mainPageDao.getLately7DayEc(front1Day);
			TheDayEc front1DayEc = new TheDayEc();
			if(ecDayInfoDOfront1DayEc != null) {
				front1DayEc.setEcDate(ecDayInfoDOfront1DayEc.getElecUseDate());
				front1DayEc.setElctrctyCnsmptn(ecDayInfoDOfront1DayEc.getElctrctyCnsmptn());
				front1DayEc.setOtdrTmprtr(ecDayInfoDOfront1DayEc.getOtdrTmprtr());
				front1DayEc.setRingRatio(ecDayInfoDOfront1DayEc.getRingRatio());
			}else {
				front1DayEc.setEcDate("");
				front1DayEc.setElctrctyCnsmptn(0);
				front1DayEc.setOtdrTmprtr(0);
				front1DayEc.setRingRatio(0);
			}
			lately7DayEc.setFront1DayEc(front1DayEc);
			//前两天
			c.set(Calendar.DATE,day-2); 
			String front2Day = sdf.format(c.getTime()); 
			EcDayInfoDO ecDayInfoDOfront2DayEc = mainPageDao.getLately7DayEc(front2Day);
			TheDayEc front2DayEc = new TheDayEc();
			if(ecDayInfoDOfront2DayEc != null) {
				front2DayEc.setEcDate(ecDayInfoDOfront2DayEc.getElecUseDate());
				front2DayEc.setElctrctyCnsmptn(ecDayInfoDOfront2DayEc.getElctrctyCnsmptn());
				front2DayEc.setOtdrTmprtr(ecDayInfoDOfront2DayEc.getOtdrTmprtr());
				front2DayEc.setRingRatio(ecDayInfoDOfront2DayEc.getRingRatio());
			}else {
				front2DayEc.setEcDate("");
				front2DayEc.setElctrctyCnsmptn(0);
				front2DayEc.setOtdrTmprtr(0);
				front2DayEc.setRingRatio(0);
			}
			lately7DayEc.setFront2DayEc(front2DayEc);
			//前三天
			c.set(Calendar.DATE,day-3); 
			String front3Day = sdf.format(c.getTime()); 
			EcDayInfoDO ecDayInfoDOfront3DayEc = mainPageDao.getLately7DayEc(front3Day);
			TheDayEc front3DayEc = new TheDayEc();
			if(ecDayInfoDOfront3DayEc != null) {
				front3DayEc.setEcDate(ecDayInfoDOfront3DayEc.getElecUseDate());
				front3DayEc.setElctrctyCnsmptn(ecDayInfoDOfront3DayEc.getElctrctyCnsmptn());
				front3DayEc.setOtdrTmprtr(ecDayInfoDOfront3DayEc.getOtdrTmprtr());
				front3DayEc.setRingRatio(ecDayInfoDOfront3DayEc.getRingRatio());
			}else {
				front3DayEc.setEcDate("");
				front3DayEc.setElctrctyCnsmptn(0);
				front3DayEc.setOtdrTmprtr(0);
				front3DayEc.setRingRatio(0);
			}
			lately7DayEc.setFront3DayEc(front3DayEc);
			//前四天
			c.set(Calendar.DATE,day-4); 
			String front4Day = sdf.format(c.getTime()); 
			EcDayInfoDO ecDayInfoDOfront4DayEc = mainPageDao.getLately7DayEc(front4Day);
			TheDayEc front4DayEc = new TheDayEc();
			if(ecDayInfoDOfront4DayEc != null) {
				front4DayEc.setEcDate(ecDayInfoDOfront4DayEc.getElecUseDate());
				front4DayEc.setElctrctyCnsmptn(ecDayInfoDOfront4DayEc.getElctrctyCnsmptn());
				front4DayEc.setOtdrTmprtr(ecDayInfoDOfront4DayEc.getOtdrTmprtr());
				front4DayEc.setRingRatio(ecDayInfoDOfront4DayEc.getRingRatio());
			}else {
				front4DayEc.setEcDate("");
				front4DayEc.setElctrctyCnsmptn(0);
				front4DayEc.setOtdrTmprtr(0);
				front4DayEc.setRingRatio(0);
			}
			
			lately7DayEc.setFront4DayEc(front4DayEc);
			//前五天
			c.set(Calendar.DATE,day-5); 
			String front5Day = sdf.format(c.getTime()); 
			EcDayInfoDO ecDayInfoDOfront5Day = mainPageDao.getLately7DayEc(front5Day);
			TheDayEc front5DayEc = new TheDayEc();
			if(ecDayInfoDOfront5Day != null) {
				front5DayEc.setEcDate(ecDayInfoDOfront5Day.getElecUseDate());
				front5DayEc.setElctrctyCnsmptn(ecDayInfoDOfront5Day.getElctrctyCnsmptn());
				front5DayEc.setOtdrTmprtr(ecDayInfoDOfront5Day.getOtdrTmprtr());
				front5DayEc.setRingRatio(ecDayInfoDOfront5Day.getRingRatio());
			}else {
				front5DayEc.setEcDate("");
				front5DayEc.setElctrctyCnsmptn(0);
				front5DayEc.setOtdrTmprtr(0);
				front5DayEc.setRingRatio(0);
			}
			lately7DayEc.setFront5DayEc(front5DayEc);
			//前六天
			c.set(Calendar.DATE,day-6); 
			String front6Day = sdf.format(c.getTime()); 
			EcDayInfoDO ecDayInfoDOfront6DayEc = mainPageDao.getLately7DayEc(front6Day);
			TheDayEc front6DayEc = new TheDayEc();
			if(ecDayInfoDOfront6DayEc != null) {
				front6DayEc.setEcDate(ecDayInfoDOfront6DayEc.getElecUseDate());
				front6DayEc.setElctrctyCnsmptn(ecDayInfoDOfront6DayEc.getElctrctyCnsmptn());
				front6DayEc.setOtdrTmprtr(ecDayInfoDOfront6DayEc.getOtdrTmprtr());
				front6DayEc.setRingRatio(ecDayInfoDOfront6DayEc.getRingRatio());
			}else {
				front6DayEc.setEcDate("");
				front6DayEc.setElctrctyCnsmptn(0);
				front6DayEc.setOtdrTmprtr(0);
				front6DayEc.setRingRatio(0);
			}
			lately7DayEc.setFront6DayEc(front6DayEc);
			logger.info(">>>>>>>>>>最近七天用电情况数据查询成功。");
			getLatelyInfoRes.setLately7DayEc(lately7DayEc);
			
			/**
			 * 今日负荷曲线
			 */
			List<LoadDayInfoDO> loadDayInfoDOList = mainPageDao.getThisDayLoad(sdf.format(date));
			List<ThisDayLoad> thisDayLoadList = new ArrayList<ThisDayLoad>();
			for(LoadDayInfoDO loadDayInfoDO : loadDayInfoDOList) {
				ThisDayLoad thisDayLoad = new ThisDayLoad();
				thisDayLoad.setElecUseDate(loadDayInfoDO.getElecUseDate());
				thisDayLoad.setStartTime(thisDayLoad.getStartTime());
				thisDayLoad.setEndTime(thisDayLoad.getEndTime());
				thisDayLoad.setMaxLoad(thisDayLoad.getMaxLoad());
				thisDayLoadList.add(thisDayLoad);
			}
			logger.info(">>>>>>>>>>今日负荷曲线数据查询成功。");
			getLatelyInfoRes.setThisDayLoadList(thisDayLoadList);
			
		} catch (Exception e) {
			logger.error("主页面最近用电负荷数据查询异常:{MainPageServiceImpl.getLateLyInfo}",e);
			e.printStackTrace();
		}
		return getLatelyInfoRes;
	}
	
}
